import os
import unittest
from classes import dict_class


# 假设以下是你测试的两个示例类
class ValidClass:

    def __init__(self, param1: int = None, param2: str = None):
        pass


class InvalidClass:

    def __init__(self, param1, param2):  # 参数没有默认值且无类型注解
        pass


# 模拟 from_dict 和 to_dict 方法
def from_dict_for_class_annotation(cls, d):
    return cls()


def to_dict_for_class_annotation(self):
    return {}


# 单元测试类
class TestDictClassDecorator(unittest.TestCase):

    def test_valid_class_with_from_dict_to_dict(self):
        """测试一个有效类，带有from_dict和to_dict方法"""

        @dict_class()
        class MyClass:
            pass

        # 确保添加了from_dict和to_dict方法
        self.assertTrue(hasattr(MyClass, 'from_dict'))
        self.assertTrue(hasattr(MyClass, 'to_dict'))

        # 确保方法是可调用的
        instance = MyClass.from_dict({})
        self.assertIsInstance(instance, MyClass)
        self.assertEqual(instance.to_dict(), {})

    def test_valid_class_without_from_dict(self):
        """测试一个有效类，没有from_dict方法"""

        @dict_class(from_dict=False)
        class MyClass:
            pass

        # 确保没有添加from_dict方法
        self.assertFalse(hasattr(MyClass, 'from_dict'))
        self.assertTrue(hasattr(MyClass, 'to_dict'))

    def test_valid_class_without_to_dict(self):
        """测试一个有效类，没有to_dict方法"""

        @dict_class(to_dict=False)
        class MyClass:
            pass

        # 确保没有添加to_dict方法
        self.assertTrue(hasattr(MyClass, 'from_dict'))
        self.assertFalse(hasattr(MyClass, 'to_dict'))

    def test_invalid_class(self):
        """测试一个无效类，其__init__方法参数不满足条件"""
        with self.assertRaises(TypeError):

            @dict_class()
            class InvalidClass:

                def __init__(self, param1, param2):  # 参数没有默认值且无类型注解
                    pass

    def test_only_class_annotation(self):
        """测试仅使用类注解的情况"""

        @dict_class(only_class_annotation=True)
        class MyClass:

            def __init__(self, param1: int = None, param2: str = None):
                pass

        instance = MyClass.from_dict({})
        self.assertIsInstance(instance, MyClass)

        with self.assertRaises(AttributeError):
            _ = instance.to_dict()


if __name__ == '__main__':
    unittest.main()
